#!/bin/bash
# Author: Omar Vega Ramos
# E-mail: ovruni@gnu.org.pe
# License: GNU GPL - GNU General Public License v3.0 or later
# http://www.gnu.org/licenses/gpl.html

#Checking packages in libre repo outdated
echo -e "\033[40m\033[1;32m==>\033[0m \033[40m\033[1;37mChecking packages...\033[0m"
MIRROR="http://mirrors.kernel.org/archlinux/"
REPOS=("core" "extra" "community")

(expac -Ss '%r/%n-%v' | grep "^libre/") > "/tmp/packages.txt"
sed -i -- 's/^libre\///g' "/tmp/packages.txt"

sed -i -- 's/\.parabola[1-9]//g' "/tmp/packages.txt"
sed -i -- 's/\.basekernel[2-4]\.[0-9]*//g' "/tmp/packages.txt"
sed -i -- '/^icedove-*/d' "/tmp/packages.txt"
sed -i -- '/^icecat-*/d' "/tmp/packages.txt"
sed -i -- '/^iceweasel-*/d' "/tmp/packages.txt"
sed -i -- '/^linux-libre*/d' "/tmp/packages.txt"
sed -i -- 's/\.[1-9]$//g' "/tmp/packages.txt"
#sed -i -- 's/-libre//g' "/tmp/packages.txt"
rm -f "/tmp/archpackages.txt"

for repo in ${REPOS[@]}; do
    wget -q -O "/tmp/$repo.db.tar.gz" "$MIRROR/$repo/os/x86_64/$repo.db.tar.gz"
    tar -ztf "/tmp/$repo.db.tar.gz" --exclude '*/*' >> "/tmp/archpackages.txt"
    rm -f "/tmp/$repo.db.tar.gz"
done

sed -i -- 's/\/$//g' "/tmp/archpackages.txt"

echo "Parabola    Arch"
echo "----------------"

while read package
do
    packname=${package%-*-*}
    packver=${package#$packname-}
    res=$(cat "/tmp/archpackages.txt" | grep -e "^$packname-[0-9\-\.]")
    
    if [ "$res" != "" ]; then
	if [ "$package" != "$res" ]; then
	    echo "$package   $res"
	fi
    fi
done < "/tmp/packages.txt"
